package com.ruoyi.system.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.vo.SysCommentResp;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.SysCommentMapper;
import com.ruoyi.system.domain.SysComment;
import com.ruoyi.system.service.ISysCommentService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 评论Service业务层处理
 * 
 * @author dht
 * @date 2025-04-28
 */
@Service
public class SysCommentServiceImpl implements ISysCommentService 
{
    @Autowired
    private SysCommentMapper sysCommentMapper;
    @Autowired
    private ISysUserService userService;

    /**
     * 查询评论
     * 
     * @param id 评论主键
     * @return 评论
     */
    @Override
    public SysComment selectSysCommentById(Long id)
    {
        return sysCommentMapper.selectSysCommentById(id);
    }

    /**
     * 查询评论列表
     * 
     * @param sysComment 评论
     * @return 评论
     */
    @Override
    public List<SysCommentResp> selectSysCommentList(SysComment sysComment)
    {
        return sysCommentMapper.selectSysCommentList(sysComment).stream().map(item -> {
            SysCommentResp resp = BeanUtil.copyProperties(item, SysCommentResp.class);
            SysUser sysUser = userService.selectUserById(item.getUserId());
            resp.setAvatar(sysUser.getAvatar());
            resp.setNickname(sysUser.getNickName());
            resp.setChildList(new ArrayList<>());
            return resp;
        }).collect(Collectors.toList());
    }

    @Override
    public List<SysCommentResp> selectFrontCommentList(SysComment sysComment) {
        return sysCommentMapper.selectPCommentList(sysComment).stream().map(item -> {
            SysCommentResp resp = BeanUtil.copyProperties(item, SysCommentResp.class);
            SysUser sysUser = userService.selectUserById(item.getUserId());
            resp.setAvatar(sysUser.getAvatar());
            resp.setNickname(sysUser.getNickName());
            resp.setChildList(sysCommentMapper.selectChildCommentByCommPath(resp.getCommentPath())
                    .stream().filter(cItem -> !cItem.getId().equals(item.getId())).map(cItem -> {
                        SysCommentResp cResp = BeanUtil.copyProperties(cItem, SysCommentResp.class);
                        SysUser cSysUser = userService.selectUserById(cItem.getUserId());
                        cResp.setAvatar(cSysUser.getAvatar());
                        cResp.setNickname(cSysUser.getNickName());
                        cResp.setChildList(new ArrayList<>());
                        if (Objects.nonNull(cItem.getpId())) {
                            if (cItem.getpId().equals(item.getId())) {
                                cResp.setReplyNickname(sysUser.getNickName());
                            } else {
                                Long userId = selectSysCommentById(cItem.getpId()).getUserId();
                                cResp.setReplyNickname(userService.selectUserById(userId).getNickName());
                            }
                        }
                        return cResp;
                    }).collect(Collectors.toList()));
            return resp;
        }).collect(Collectors.toList());
    }

    /**
     * 新增评论
     * 
     * @param sysComment 评论
     * @return 结果
     */
    @Override
    @Transactional
    public int insertSysComment(SysComment sysComment)
    {
        sysComment.setCreateTime(DateUtils.getNowDate());
        sysComment.setUserId(SecurityUtils.getUserId());
        sysComment.setLikeNum(0);
        int i = sysCommentMapper.insertSysComment(sysComment);
        if (Objects.isNull(sysComment.getpId())) {
            sysComment.setCommentPath(sysComment.getId() + "/");
        } else {
            SysComment pComment = selectSysCommentById(sysComment.getpId());
            sysComment.setCommentPath(pComment.getCommentPath() + sysComment.getId() + "/");
        }
        sysCommentMapper.updateSysComment(sysComment);
        return i;
    }

    /**
     * 修改评论
     * 
     * @param sysComment 评论
     * @return 结果
     */
    @Override
    public int updateSysComment(SysComment sysComment)
    {
        sysComment.setUpdateTime(DateUtils.getNowDate());
        return sysCommentMapper.updateSysComment(sysComment);
    }

    /**
     * 批量删除评论
     * 
     * @param ids 需要删除的评论主键
     * @return 结果
     */
    @Override
    public int deleteSysCommentByIds(Long[] ids)
    {
        return sysCommentMapper.deleteSysCommentByIds(ids);
    }

    /**
     * 删除评论信息
     * 
     * @param id 评论主键
     * @return 结果
     */
    @Override
    public int deleteSysCommentById(Long id)
    {
        return sysCommentMapper.deleteSysCommentById(id);
    }

    @Override
    public int selectCount() {
        return sysCommentMapper.selectCount();
    }
}
